#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./esb/components/bk/apis/cc/edit_app.py
# Compiled at: 2017-11-16 15:44:28
"""
Copyright © 2012-2017 Tencent BlueKing. All Rights Reserved. 蓝鲸智云 版权所有
"""
from django import forms
from common.forms import BaseComponentForm, ListField
from common.constants import API_TYPE_OP
from components.component import Component
from .toolkit import tools, configs

class EditApp(Component):
    """
    apiLabel 编辑业务
    apiMethod POST
    
    ### 功能描述
    
    编辑业务
    
    ### 请求参数
    
    {{ common_args_desc }}
    
    #### 接口参数
    
    | 字段      |  类型      | 必选   |  描述      |
    |-----------|------------|--------|------------|
    | app_id    | int        | 是     | 业务ID |
    | app_name  | string     | 否     | 业务名 |
    | maintainers | string   | 否     | 运维人员, 多个人之间用逗号分隔 |
    | product_pm | string    | 否     | 产品人员，多个人之间用逗号分隔 |
    | developer | string     | 否     | 开发人员，多个人之间用逗号分隔 |
    | tester    | string     | 否     | 测试人员，多个人之间用逗号分隔 |
    | operator  | string     | 否     | 操作者，多个人之间用逗号分隔   |
    | life_cycle | string    | 否     | 生成周期，测试中, 已上线, 停运其中的一个值 |
    
    ### 请求参数示例
    
    ```python
    {
        "app_code": "esb_test",
        "app_secret": "xxx",
        "bk_token": "xxx",
        "app_id": 1,
        "app_name": "测试",
        "operator": "test1,test2"
    }
    ```
    
    ### 返回结果示例
    
    ```python
    {
        "result": true,
        "code": "00",
        "message": "",
        "data": {},
    }
    ```
    """
    sys_name = configs.SYSTEM_NAME
    api_type = API_TYPE_OP
    host = configs.host

    class Form(BaseComponentForm):
        app_id = forms.IntegerField(label=u'业务ID', required=True)
        app_name = forms.CharField(label=u'业务名', required=False)
        maintainers = ListField(label=u'运维人员', required=False)
        product_pm = ListField(label=u'产品人员', required=False)
        developer = ListField(label=u'开发人员', required=False)
        tester = ListField(label=u'测试人员', required=False)
        operator = ListField(label=u'操作者', required=False)
        life_cycle = forms.CharField(label=u'生成周期', required=False)

        def clean(self):
            cleaned_data = self.cleaned_data
            data = self.data
            to_cc_key_map = {'app_id': 'ApplicationID',
               'app_name': 'ApplicationName',
               'maintainers': 'Maintainers',
               'product_pm': 'ProductPm',
               'developer': 'Developer',
               'tester': 'Tester',
               'operator': 'Operator',
               'life_cycle': 'LifeCycle'
               }
            params = {}
            for bk_key, cc_key in to_cc_key_map.iteritems():
                if data.get(bk_key) is not None:
                    if bk_key in ('maintainers', 'product_pm', 'developer', 'tester',
                                  'operator'):
                        params[cc_key] = (',').join(cleaned_data[bk_key])
                    else:
                        params[cc_key] = cleaned_data[bk_key]

            return params

    def handle(self):
        client = tools.CCClient(self.outgoing.http_client)
        self.response.payload = client.post_request(self.host, '/api/app/editApp', data=self.form_data)
